/**
 * @file ethercat_config.c
 * @brief EtherCAT主站配置文件实现
 * 
 * 该文件实现了EtherCAT主站的配置选项
 */

#include "ethercat_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 默认配置文件路径 */
#define DEFAULT_CONFIG_FILE "ethercat.conf"

/* 默认主站类型 */
#define DEFAULT_MASTER_TYPE MASTER_TYPE_SOEM

/* 默认网络接口 */
#ifdef _WIN32
#define DEFAULT_INTERFACE "\\Device\\NPF_{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
#else
#define DEFAULT_INTERFACE "eth0"
#endif

EtherCATMasterType load_master_type_from_config(const char *config_file) {
    const char *filename = config_file ? config_file : DEFAULT_CONFIG_FILE;
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        printf("配置文件 %s 不存在，使用默认主站类型\n", filename);
        return DEFAULT_MASTER_TYPE;
    }
    
    char line[256];
    EtherCATMasterType type = DEFAULT_MASTER_TYPE;
    
    while (fgets(line, sizeof(line), fp)) {
        /* 去除行尾的换行符 */
        size_t len = strlen(line);
        if (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) {
            line[len-1] = '\0';
        }
        
        /* 解析主站类型 */
        if (strncmp(line, "master_type=", 12) == 0) {
            const char *value = line + 12;
            if (strcmp(value, "igh") == 0) {
                type = MASTER_TYPE_IGH;
            } else if (strcmp(value, "soem") == 0) {
                type = MASTER_TYPE_SOEM;
            } else {
                printf("未知的主站类型: %s，使用默认类型\n", value);
            }
        }
    }
    
    fclose(fp);
    return type;
}

bool save_master_type_to_config(EtherCATMasterType type, const char *config_file) {
    const char *filename = config_file ? config_file : DEFAULT_CONFIG_FILE;
    FILE *fp = fopen(filename, "w");
    if (!fp) {
        printf("无法创建配置文件 %s\n", filename);
        return false;
    }
    
    /* 写入主站类型 */
    const char *type_str = (type == MASTER_TYPE_IGH) ? "igh" : "soem";
    fprintf(fp, "# EtherCAT主站配置文件\n");
    fprintf(fp, "master_type=%s\n", type_str);
    
    /* 写入默认网络接口 */
    fprintf(fp, "interface=%s\n", DEFAULT_INTERFACE);
    
    fclose(fp);
    return true;
}

bool get_default_interface(char *ifname, size_t size) {
    if (!ifname || size == 0) {
        return false;
    }
    
    /* 尝试从配置文件读取 */
    FILE *fp = fopen(DEFAULT_CONFIG_FILE, "r");
    if (fp) {
        char line[256];
        while (fgets(line, sizeof(line), fp)) {
            /* 去除行尾的换行符 */
            size_t len = strlen(line);
            if (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) {
                line[len-1] = '\0';
            }
            
            /* 解析网络接口 */
            if (strncmp(line, "interface=", 10) == 0) {
                const char *value = line + 10;
                strncpy(ifname, value, size - 1);
                ifname[size - 1] = '\0';
                fclose(fp);
                return true;
            }
        }
        fclose(fp);
    }
    
    /* 使用默认值 */
    strncpy(ifname, DEFAULT_INTERFACE, size - 1);
    ifname[size - 1] = '\0';
    return true;
}